package Excuse.stack;

public class StackArray {

    //栈最大的长度
    private  int maxstack;

    //定义数组栈
    private  int []stack;

    //定义栈顶指针
    private  int top=-1;

    public StackArray(int maxstack) {
        this.maxstack = maxstack;
        stack=new int[maxstack];
    }

    /***
     * 1.压栈
     * 2.出栈
     * 3.判断栈是否为空
     * 4.判断栈是否满
     */

    //判断栈是否为空
    public   boolean isEmpty(){
        return  this.top==-1;
    }

    //判断栈是否满
    public   boolean isFull(){
        return  this.top==this.maxstack-1;
    }

    //入栈
    public void push(int var){
        //判断栈是否满
        if(isFull()){
            throw new RuntimeException("栈已满，无法入栈");
        }
        top++;
        stack[top]=var;
    }
    //出栈
    public int pop(){
        if(isEmpty()){
            throw new RuntimeException("栈为空，出栈失败");
        }

        int var=stack[top];
        top--;
        return  var;
    }
    public  int length(){
        return this.top+1;
    }

    //判断是否是字符
    public  boolean isOper(char v){
        return  v=='+'||v=='-'||v=='*'||v=='/';
    }

    //判断优先级
    public int prority(char v){
        if(v=='*'||v=='/'){
            return  1;
        }else  if(v=='+'||v=='-'){
            return  0;
        }else {
            return  -1;
        }
    }
    //返回栈顶元素
    public  int  peek(){
        return  stack[top];
    }

    //返回堆栈长度
    public  int stackLength(){
        return this.stack.length;
    }

    //计算
    public  int calculate(int num1,int num2,char oper){
        int result=0;
        switch (oper){
            case '+':
                result=num1+num2;
                break;
            case '-':
                result=num2-num1;
                break;
            case '*':
                result=num1*num2;
                break;
            case '/':
                result=num2/num1;
                break;
        }
        return  result;
    }
}
